﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Card.Web.Member
{
    public partial class Address : MemberBase
    {
        #region

        #endregion

        #region
        protected override void OnInit(EventArgs e)
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            base.OnInit(e);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (Utility.ValidationHelper.IsNumber(e.CommandArgument.ToString()))
            {
                int addressID = int.Parse(e.CommandArgument.ToString());
                if (string.Compare(e.CommandName, "M") == 0)
                {
                    this.BindAddress(addressID);
                }
                else if (string.Compare(e.CommandName, "D") == 0)
                {
                    if (BLL.AddressManager.Delete(addressID, this.Passport))
                    {
                        this.MessageBox("删除成功！");
                        this.DoInit();
                    }
                }
            }
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            if (this.DoCheck() && !this.IsRefresh)
            {
                Entity.Address address = null;
                if (!string.IsNullOrEmpty(this.hdnAddressID.Value) && Utility.ValidationHelper.IsNumber(this.hdnAddressID.Value))
                {
                    address = BLL.AddressManager.GetEntity(int.Parse(this.hdnAddressID.Value), this.Passport);
                }

                if (address == null)
                {
                    address = new Card.Entity.Address();
                    address.ID = BAF.BLL.SYSSequenceManager.GetSequence(CONST.SEQUENCE_CODE.ADDRESS_ID,null);
                }
                address.UserID = this.Passport.UserID;
                address.ReceiverName = this.txtReceiver.Text;
                address.Area = this.txtArea.Text;
                address.Street = this.txtStreet.Text;
                address.PostCode = this.txtPostCode.Text;
                address.Phone = this.txtTel.Text;
                if (BLL.AddressManager.Save(address, this.Passport))
                {
                    this.MessageBox(string.Format("{0}成功！",this.btnSave.Text));

                    this.DoInit();
                }

            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }

        #endregion

        #region
        private void DoInit()
        {            
            this.rptList.DataSource = BLL.AddressManager.GetListByUserID(this.Passport.UserID, this.Passport);
            this.rptList.DataBind();
            this.ClearInput();
        }

        private bool DoCheck()
        {
            if (string.IsNullOrEmpty(this.txtReceiver.Text))
            {
                this.MessageBox("收货人姓名不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtArea.Text))
            {
                this.MessageBox("省市区/县不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtStreet.Text))
            {
                this.MessageBox("乡镇/街道不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtPostCode.Text))
            {
                this.MessageBox("邮政编码不能为空！");
                return false;
            }

            if (string.IsNullOrEmpty(this.txtTel.Text))
            {
                this.MessageBox("联系电话不能为空！");
                return false;
            }

            return true;
        }

        private void BindAddress(int addressID)
        {
            Entity.Address address = BLL.AddressManager.GetEntity(addressID, this.Passport);
            if (address != null)
            {
                this.hdnAddressID.Value = address.ID.ToString();
                this.txtReceiver.Text = address.ReceiverName;
                this.txtArea.Text = address.Area;
                this.txtStreet.Text = address.Street;
                this.txtPostCode.Text = address.PostCode;
                this.txtTel.Text = address.Phone;
                this.btnSave.Text = "修改";
            }
        }

        private void ClearInput()
        {
            this.hdnAddressID.Value =
            this.txtReceiver.Text =
            this.txtArea.Text =
            this.txtStreet.Text =
            this.txtPostCode.Text =
            this.txtTel.Text = string.Empty;
            this.btnSave.Text = "保存";
        }
        #endregion
    }
}
